from marshmallow import Schema
from flask_restplus import Namespace, fields
from app.models.user import User


class UserSchemas(object):
    def __init__(self, namespace):
        self.namespace = namespace

    def role_body(self):
        return self.namespace.model(
            "user_role",
            {
                "id": fields.Integer(),
                "role_name": fields.String(),
            },
        )

    def user_token(self):
        return self.namespace.model(
            "user_token", {"username": fields.String(), "token": fields.String()}
        )

    def user_login_query(self):
        return self.namespace.model(
            "user_login_query",
            {
                "username": fields.String(max_length=256, required=True),
                "password": fields.String(max_length=256, required=True),
            },
        )

    def user_list_query(self):
        return {"page": "页码", "per_page": "页容量", "role_name": "用户角色"}

    def user_son_path_body(self):
        return self.namespace.model(
            "path_son_body",
            {
                "id": fields.Integer(),
                "path": fields.String(),
                "path_name": fields.String(),
                "icon": fields.String(),
            },
        )

    def user_path_body(self):
        return self.namespace.model(
            "path_body",
            {
                "id": fields.Integer(),
                "path": fields.String(),
                "path_name": fields.String(),
                "icon": fields.String(),
                "son": fields.List(fields.Nested(self.user_son_path_body())),
            },
        )

    def user_roles_body(self):
        return self.namespace.model(
            "user_order_body",
            {
                "id": fields.Integer(),
                "role_name": fields.String(),
                "path": fields.List(fields.Nested(self.user_path_body())),
            },
        )

    def user_info_body(self):
        return self.namespace.model(
            "user_info",
            {
                "id": fields.Integer(),
                "username": fields.String(),
                "super_user": fields.Boolean(),
                "roles": fields.Nested(self.user_roles_body()),
            },
        )

    def user_list_body(self):
        return self.namespace.model(
            "user_list",
            {
                "total": fields.Integer(),
                "data": fields.List(fields.Nested(self.user_info_body())),
            },
        )

    def user_registr_query(self):
        return self.namespace.model(
            "user_registr",
            {
                "username": fields.String(max_length=256, required=True),
                "password": fields.String(max_length=256, required=True),
                "password2": fields.String(max_length=256, required=True),
                "role": fields.String(max_length=256, required=True),
            },
        )

    def user_registr_body(self):
        return self.namespace.model("user_body", {"id": fields.Integer()})
